package com.javandad.app.client.searchComponent;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;
import com.javandad.app.shared.entity.search.AdvancedUserSearchProperties;
import com.javandad.app.shared.entity.search.SearchProperties;

class UserSearch extends Search {
	private final TextBox countries = new TextBox();
	private final TextBox cities = new TextBox();
	private final TextBox email = new TextBox();
	private final DateBox creationDateStart = new DateBox();
	private final DateBox creationDateEnd = new DateBox();

	FlexTable opts = new FlexTable();
	
	public UserSearch(TextBox freetext) {
		super(freetext);
		initAdvancedUserSearch();
	}

	private void initAdvancedUserSearch() {
		opts.setText(0, 0, "Countries:");
		opts.setWidget(0, 1, countries);
		opts.setText(1, 0, "Cities:");
		opts.setWidget(1, 1, cities);
		opts.setText(2, 0, "Email:");
		opts.setWidget(2, 1, email);
		opts.setText(3, 0, "Creation date:");
		FlexTable creationDate = new FlexTable();
		DateTimeFormat dateFormat = DateTimeFormat.getFormat(PredefinedFormat.DATE_MEDIUM);
		creationDateStart.setFormat(new DateBox.DefaultFormat(dateFormat));
		creationDateEnd.setFormat(new DateBox.DefaultFormat(dateFormat));
		creationDate.setText(0, 0, "from");
		creationDate.setWidget(0, 1, creationDateStart);
		creationDate.setText(1, 0, "until");
		creationDate.setWidget(1, 1, creationDateEnd);
		opts.setWidget(3, 1, creationDate);
	}
	
	@Override
	public Widget getAdvancedOptions() {
		return opts;
	}

	@Override
	public SearchProperties getSearchProperties() throws InvalidArgumentException {
		// freetext:
		String freetext = freetextSearchTerm.getText();

		// TEST:
		if (freetext.equals("jondoe"))
			throw new InvalidArgumentException("Don't be lazy, enter something different! ;P\n(this is for testing the InvalidArgumentException.)");

		// advanced user search options:
		Date creationDateStart = this.creationDateStart.getValue();
		Date creationDateEnd = this.creationDateEnd.getValue();
		String email = this.email.getText();
		if(email.equals(""))
			email = null;
		List<String> countries = new ArrayList<String>();
		String[] countriesArr = this.countries.getText().split(",");
		for (int i = 0; i < countriesArr.length; i++)
			if(!countriesArr[i].equals(""))
				countries.add(countriesArr[i]);
		if(countries.isEmpty())
			countries = null;
		List<String> cities = new ArrayList<String>();
		String[] citiesArr = this.cities.getText().split(",");
		for (int i = 0; i < citiesArr.length; i++)
			if(!citiesArr[i].equals(""))
				cities.add(citiesArr[i]);
		if(cities.isEmpty())
			cities = null;

		AdvancedUserSearchProperties ausp = new AdvancedUserSearchProperties(freetext, creationDateStart, creationDateEnd, email, countries, cities);
		
//		if(freetext.equals("") && !ausp.anythingSet())
//			throw new InvalidArgumentException("You have to type something to search for...");
		
		return ausp;
	}
	
	@Override
	public String getSearchName() {
		return "Users";
	}
}